package com.sky.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface AddressBookMapper extends BaseMapper<AddressBook> {
    /**
     * 统计数量
     */
    @Select("select count(*) from address_book")
    Integer count();

    /**
     * 查询所有地址
     *
     * @return
     */
    @Select("select id,user_id,consignee,sex,phone,province_code,province_name,city_code,city_name,district_code,district_name,detail,label,is_default" +
            " from address_book where user_id = #{id}")
    List<AddressBook> list(Long id);

    /**
     * 设置默认地址
     *
     * @param id
     */
    @Update("update address_book set is_default = 1 where id = #{id}")
    void setDefault(Long id);

    /**
     * 取消其他地址的默认状态
     *
     * @param userId
     * @param userId
     */
    @Update("update address_book set is_default = 0 where user_id = #{userId}")
    void cancelOthersDefault(Long userId);

    /**
     * 查询默认地址
     *
     * @param currentId
     * @return
     */
    @Select("select id,user_id,consignee,sex,phone,province_code,province_name,city_code,city_name,district_code,district_name,detail,label,is_default" +
            " from address_book where is_default = 1")
    AddressBook getDefault(Long currentId);
}
